
import { initSearchConfig, TableSearchPanel, XxpageContainer, XxTable } from "@/ui";
import React, { useEffect, useRef, useState } from "react";
import { getSearchFields, getActoinPanel, getColumns } from "./config";
import EditForm from "./editForm";
import { devApi as api } from "@/api";
import { deleteConfirm, successAlert } from "@/common/methods";


/**@description 【会员等级】页面 */
const CodeGengeration = () => {
    const searchFormRef = useRef(null); //搜索表单的引用对象，用于获取/操作表单实例
    const editFormRef = useRef(null); //用于引用编辑表单的 ref 对象
    const [params, setParams] = useState({ ...initSearchConfig(getSearchFields({ searchFormRef })) })
    const [state, setState] = useState({
        data: [],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找列表 */
    const getList = async () => {
        try {
            setLoading(true);
            const { data, code, msg } = await api.queryList({ pageSize: 10, current: 1, ...params });
            setState({
                data: [...data.list],
                totalCount: data.totalCount || 0,
            })
        } catch (error) {
        }finally{
            setLoading(false);
        }
    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }
    useEffect(() => {
        getList()
    }, [params])

    /**  @description 打开编辑菜单窗口 */
    const open = ({ type = "add", record = {} }) => {
        editFormRef.current.open({ type, record });
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (object) => {
        try {
            await deleteConfirm();
            const { code, msg } = await api.delete(object.id)
        } catch { } finally {
            getList()
        }
    }


    const [selectedRows, setSelectRows] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectRows(selectedRows);
    }
    const actionPanel = getActoinPanel({
        open: open
    })
    const tableProps = {
        hideActionPanel: false,
        tablePanelConfig: actionPanel,
        loading,
        columns: getColumns({ open, handleDelete }),
        dataSource: state.data,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current

        },
        rowSelection: {
            selectedRowKeys: selectedRows.map(item => item._id),
            onChange: onSelectChange,
        },
        rowKey: 'id',
        selectRecords: selectedRows,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {

                }
            }
        },
    }


    return (
        <XxpageContainer>
            <EditForm onRef={editFormRef} getList={getList} />
            <TableSearchPanel
                onRef={searchFormRef}
                fields={getSearchFields({ searchFormRef })}
                onSearch={onClickSearch}
            />
            <XxTable  {...tableProps} />

        </XxpageContainer>
    )
}

export default CodeGengeration;
